package com.qinghua.blog.controller.admin;

import com.qinghua.blog.po.Type;
import com.qinghua.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("admin")
public class TypeController {

    @Autowired
    private TypeService typeService;

    /**
     * 查询所有类型并分页返回
     * @param pageable
     * @param model
     * @return
     */
    @GetMapping("/types")
    public String typeList(@PageableDefault(size = 10,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model){

        model.addAttribute("page",typeService.listType(pageable));

        return "admin/types";
    }

    /**
     * 跳转到类型新增页面
     * @param model
     * @return
     */
    @GetMapping("/types/input")
    public String input(Model model){

        model.addAttribute("type",new Type());

        return "admin/type_input";

    }

    /**
     * 根据id查询到类型信息并且返回到修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/types/{id}/input")
    public String editInput(@PathVariable Long id, Model model){

        model.addAttribute("type",typeService.getType(id));

        return "admin/type_input";
    }

    /**
     * 新增类型
     * @param type 接受传递来的类型,并且属性有非空验证 需要加注解 @Valid
     * @param result 用来传递type属性name上的错误信息
     * @param attributes
     * @return
     */
    @PostMapping("/types")
    public String saveTypes(@Valid Type type, BindingResult result, RedirectAttributes attributes){

        if(typeService.getTypeByName(type.getName())!=null){

            result.rejectValue("name","nameError","此类型已存在");
            //attributes.addFlashAttribute("msg","已存在此类型");

            return "admin/type_input";
        }

        if(result.hasErrors()){
            return "admin/type_input";
        }

        Type t=typeService.saveType(type);
        if(t!=null){
            attributes.addFlashAttribute("msg","新增成功");
        }else {
            attributes.addFlashAttribute("msg","新增失败");
        }
        return "redirect:/admin/types";
    }

    /**
     * 用于修改类型
     * @param type
     * @param result
     * @param id
     * @param attributes
     * @return
     */
    @PostMapping("/types/{id}")
    public String editPost(@Valid Type type, BindingResult result,@PathVariable Long id,RedirectAttributes attributes){


        if(typeService.getTypeByName(type.getName())!=null){

            result.rejectValue("name","nameError","此类型已存在");
            //attributes.addFlashAttribute("msg","已存在此类型");

            return "admin/type_input";
        }

        //如name果属性为空
        if(result.hasErrors()){
            return "admin/type_input";
        }

        //从路径里获取到id并且设置到类型里边
        type.setId(id);

        Type t=typeService.updateType(id,type);
        if(t!=null){
            attributes.addFlashAttribute("msg","修改成功");
        }else {
            attributes.addFlashAttribute("msg","修改失败");
        }
        return "redirect:/admin/types";
    }

    /**
     * 根据id删除类型
     * @param id
     * @param attributes
     * @return
     */
    @GetMapping("/types/{id}/delete")
    public String deleteType(@PathVariable Long id,RedirectAttributes attributes){

        typeService.deleteType(id);

        attributes.addFlashAttribute("msg","删除成功");
        return "redirect:/admin/types";
    }

}
